import 'package:flutter/cupertino.dart';
import 'package:flutter_module/common/extensions/cached_network_image_extension.dart';
import 'package:flutter_module/common/widget/swiper_pagination.dart';
import 'package:flutter_swiper_null_safety/flutter_swiper_null_safety.dart';

/// 合作服务商banner组件
class ServiceProviderBanner extends StatelessWidget {
  const ServiceProviderBanner(
      {Key? key,
      this.bannerImgUrls = const [],
      this.bannerPlaceholder = ImagePlaceholder.largePlaceholder,
      this.showEdit = true,
      this.autoplay = true,
      this.singleItemWithoutLoop = true,
      this.hidesPaginationForSingleItem = true,
      this.fit,
      this.onEdit,
      this.onTap})
      : super(key: key);
  final List<String> bannerImgUrls;
  final String bannerPlaceholder;
  final bool showEdit;
  final void Function()? onEdit;
  final void Function(int index)? onTap;
  final bool autoplay;
  final bool singleItemWithoutLoop;
  final bool hidesPaginationForSingleItem;
  final BoxFit? fit;

  bool get _autoplay {
    return singleItemWithoutLoop && bannerImgUrls.length <= 1
        ? false
        : autoplay;
  }

  bool get _loop {
    return singleItemWithoutLoop && bannerImgUrls.length <= 1 ? false : true;
  }

  SwiperPlugin? get _pagination {
    if (hidesPaginationForSingleItem && bannerImgUrls.length <= 1) {
      return null;
    }
    return SwiperPagination(
      alignment: Alignment.bottomCenter,
      margin: EdgeInsets.only(bottom: 15),
      builder: CustomPaginationBuilder(
        size: const Size(10, 3),
        activeSize: const Size(10, 3),
        activeColor: const Color(0xff333333),
        color: const Color(0x7fffffff),
        radius: 1.5,
      ),
    );
  }

  Widget get _swiper {
    if (bannerImgUrls.length == 0 && bannerPlaceholder.isNotEmpty) {
      return SizedBox.expand(
        child: Image.asset(
          bannerPlaceholder,
          fit: BoxFit.cover,
        ),
      );
    }
    return Swiper(
      onTap: onTap,
      autoplay: _autoplay,
      loop: _loop,
      itemCount: bannerImgUrls.length,
      pagination: _pagination,
      itemBuilder: (context, index) {
        return YFNetworkImage(
          resource: bannerImgUrls[index],
          placeholderName: bannerPlaceholder,
          fit: fit,
        );
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: MediaQuery.of(context).size.width,
      height: 153.0 / 375.0 * MediaQuery.of(context).size.width,
      child: Stack(
        children: [
          _swiper,
          if (showEdit)
            Positioned(
              child: _buildEditButton(),
              top: 16.0,
              right: 16.0,
            ),
        ],
      ),
    );
  }

  Widget _buildEditButton() {
    return GestureDetector(
      onTap: onEdit,
      child: Container(
        width: 90.0,
        height: 40.0,
        decoration: BoxDecoration(
          color: Color(0x6f000000),
          borderRadius: BorderRadius.circular(5.0),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: [
            Text(
              '编辑图片',
              style: TextStyle(
                  color: CupertinoColors.white,
                  fontSize: 12.0,
                  fontWeight: FontWeight.bold),
            ),
            SizedBox(
              width: 6.0,
            ),
            Image.asset(
              'images/service_provider/service_provider_edit_image_icon.png',
              width: 16,
              height: 13,
            ),
          ],
        ),
      ),
    );
  }
}
